<template>
  <!-- 行政区域管理 -->
  <div class="col-wrapper" v-loading="loading" element-loading-text="拼命加载中">
    <div class="col-tree-box">
      <el-scrollbar>
        <el-tree highlight-current
                 :expand-on-click-node="false"
                 ref="tree"
                 node-key="id"
                 default-expand-all
                 :data="treeData"
                 :props="defaultProps"
                 @node-click="handleNodeClick"></el-tree>
      </el-scrollbar>
    </div>
    <div class="col-data-box">
      <el-row>
        <!-- 搜索框 -->
        <el-col :span="24">
          <div class="search-wrapper">
            <ul class="search-box">
              <li><span class="filter-name">区域编码：</span>
                <el-input style="width:150px" v-model="listQuery.regId"></el-input></li>
              <li><span class="filter-name">区域名称：</span>
                <el-input style="width:150px" v-model="listQuery.regName"></el-input></li>
              <li style="padding-left:10px;"><el-button style="vertical-align:middle;margin-top:-2px;" round size="mini" type="primary" @click="getList">查询</el-button></li>
            </ul>
          </div>
        </el-col>
        <!-- 按钮区 -->
        <el-col :span="24">
          <div class="button-wrapper">
            <el-button size="mini" type="success" round  @click="addRegion">新增</el-button>
            <el-button size="small" type="danger" round @click="batchEdit(-1)">删除</el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-row>
        <el-col :span="24">
          <div class="data-wrapper">
            <template v-if="tableData!=null">
              <el-table ref="multipleTable" :data="tableData"  tooltip-effect="dark" style="width: 100%"
                        @selection-change="handleSelectionChange">
                <el-table-column type="selection" label="全选" width="40">
                </el-table-column>
                <el-table-column type="index" width="50" label="序号" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="regId" label="行政区编码">
                </el-table-column>
                <el-table-column prop="regName" label="行政区名称">
                </el-table-column>
                <el-table-column prop="regIndex" label="排序号">
                </el-table-column>
                <el-table-column prop="pregName" label="上级行政区" :formatter="formatter">
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center" width="200">
                  <template slot-scope="scope">
                    <el-button size="small" type="primary" round @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </div>
        </el-col>
      </el-row>
    </div>

    <!--新增弹窗  -->
    <el-dialog title="区域模块管理" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="上级行政区" :label-width="formLabelWidth">
              <el-input v-model="form.pregName" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="行政区名称" :label-width="formLabelWidth" prop="regName">
              <el-input v-model="form.regName" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="行政区全名" :label-width="formLabelWidth" prop="fullName">
              <el-input placeholder="XX省XX市XX县..." v-model="form.fullName" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="排序号" :label-width="formLabelWidth" prop="regIndex">
              <el-input v-model="form.regIndex" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="medium" type="primary" @click="insert('form')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 弹出窗口区域 -->
    <el-dialog title="区域模块管理" :visible.sync="editdialogForm">
      <el-form :model="oneCol" :rules="rules" ref="oneCol" v-if="oneCol">
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="上级行政区" :label-width="formLabelWidth">
              <el-input v-model="oneCol.pregName" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="行政区编码" :label-width="formLabelWidth">
              <el-input v-model="oneCol.regId" auto-complete="off" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="行政区名称" :label-width="formLabelWidth" prop="regName">
              <el-input v-model="oneCol.regName" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="行政区全名" :label-width="formLabelWidth" prop="fullName">
              <el-input placeholder="XX省XX市XX县..." v-model="oneCol.fullName" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="排序号" :label-width="formLabelWidth" prop="regIndex">
              <el-input v-model="oneCol.regIndex" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="editdialogForm = false">取 消</el-button>
        <el-button size="medium" type="primary" @click="update('oneCol')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
    export default {
        data() {
            return {
                tableData: [],
                //查询条件
                listQuery: {
                    currentRegion:'A'
                },
                listSelect: [],
                dialogFormVisible: false,
                showDialog: false,
                editdialogForm: false,
                loading: false,
                form: {},
                oneCol: {},
                rules: {
                    regName: [
                        { required: true, message: "请输入行政区名称", trigger: "blur" }
                    ],
                    fullName: [
                        { required: true, message: "请输入行政区全名", trigger: "blur" }
                    ],
                    regIndex: [
                        { required: true, message: "请输入排序号", trigger: "blur" }
                    ]
                },
                formLabelWidth: "100px",
               // treeData: [],
                defaultProps: {
                    children: "children",
                    label: "regName"
                }
            };
        },
        mounted() {
            this.getList();
        },
        computed:{
            treeData:function(){
            return this.$store.state.dropTree
            }
        },
        methods: {
            formatter: function(row, column) {
                if(row.pregName){
                    return row.pregName;
                }else{
                    return "-";
                }
            },
            //列表查询
            getList() {
                this.loading = true;
                this.postRequest("/api/rcs/sysRegion/getCurrentRegionChirdren",this.listQuery)
                    .then(res => {

                        var data = res.data;
                        if (data.status == 200) {
                            this.tableData = data.data;
                        } else {
                            this.$message.error(data.msg);
                        }
                        this.loading = false;
                    })
                    .catch(error => {
                        this.$message.error(error);
                    });
            },
            //获取菜单树的数据
            // getTree(){
            //     this.postRequest("/api/rcs/sysRegion/getAllRegion")
            //         .then(res => {
            //             var data = res.data;
            //             if (data.status == 200) {
            //                 this.treeData = data.data;
            //             } else {
            //                 this.$message.error(data.msg);
            //             }
            //         })
            //         .catch(error => {
            //             this.$message.error(error);
            //         });
            // },
            addRegion(){
                if(this.form.regLevel === undefined){
                    this.form.pregName = "全国";
                    this.dialogFormVisible = true;
                }else if(this.form.regLevel < 5){
                    this.dialogFormVisible = true;
                }else{
                    this.$message.error("当前区域等级为5，不能在此下面新增区域！");
                }
            },
            //树节点点击事件
            handleNodeClick(data) {
                this.listQuery.currentRegion=data.regId
                this.getList()
                this.form.pregName=data.regName;
                this.form.pregId=data.regId;
                this.form.regLevel=data.regLevel;
            },
            insert(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.postRequest("/api/rcs/sysRegion",this.form).then((res)=>{
                            if(res.data.status == 200){
                                this.dialogFormVisible = false;
                                this.$message.success('新增成功！');
                                this.getList();
                                this.$store.dispatch('getDropTree')
                            }else{
                                this.$message.error("新增失败！");
                            }
                        })
                        .catch((error)=>{
                            this.$message.error(error)
                        })
                    } else {
                        this.$message.error('error submit!!');
                        return false;
                    }
                });
            },
            //单个删除
            handleDel(index, row) {
                this.$confirm("确认删除？").then(_ => {
                    this.deleteRequest("/api/menu", { resId: row.resId })
                        .then(res => {
                            var data = res.data;
                            this.$message.success(data.msg);
                            this.getList();
                            this.$store.dispatch('getDropTree')
                        })
                        .catch(error => {
                            //错误信息
                            this.$message.error(error);
                        });
                });
            },
            //更新
            update() {
                this.putRequest("/api/rcs/sysRegion", this.oneCol)
                    .then(res => {
                        var data = res.data;
                        if (data.status == 200) {
                            this.$message.success(data.msg);
                            this.editdialogForm = false;
                            this.getList();
                            this.$store.dispatch('getDropTree')
                            this.oneCol = {};
                        } else {
                            this.$message.error(data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error(error);
                    });
            },
            //批量操作
            batchEdit(val) {
                if (this.listSelect.length <= 0) {
                    this.$message.warning("请选择至少一条数据");
                    return;
                }
                var strArr = [];
                for (var i = 0; i < this.listSelect.length; i++) {
                    strArr.push(this.listSelect[i].regId);
                }

                //前面三个参数分别是 表名，状态字段名，id字段名
                //status 为状态值 （0：正常  9：停用 -1：删除）
                //ids 为需要批量操作的 id数组
                var submitData = {
                    tableName: "sys_region",
                    tableCol: "reg_state",
                    tableId: "reg_id",
                    status: val,
                    ids: strArr
                };

                this.$confirm("确认批量操作？").then(_ => {
                    this.putRequest("/api/common/batch", submitData)
                        .then(res => {
                            var data = res.data;
                            if (data.status == 200) {
                                this.$message.success(data.msg);
                                this.getList();
                                this.$store.dispatch('getDropTree')
                            } else {
                                this.$message.error(data.msg);
                            }
                        })
                        .catch(error => {
                            this.$message.error(error);
                        });
                });
            },
            handleSelectionChange(val) {
                this.listSelect = val;
            },
            indexMethod(index) {
                return index + 1;
            },
            //显示编辑弹窗
            handleEdit(index, row) {
                this.editdialogForm = true;
                this.oneCol = row;
            }
        }
    };
</script>
<style scoped>
</style>
